﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using RBAC.API.Command.Permission;

namespace RBAC.API.Handler.Permission
{
    public class PermissionDelHandler:IRequestHandler<PermissionDelCommand,ApiResult<int>>
    {
        private readonly IRepository<PermissionModel> permissionway;
        private readonly IMapper mapper;
        private readonly ILogger<PermissionGetHandler> log;

        public PermissionDelHandler(IRepository<PermissionModel> permissionway, IMapper mapper, ILogger<PermissionGetHandler> log)
        {
            this.permissionway = permissionway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 删除权限信息
        /// </summary>
        /// <param name="request">权限主键</param>
        /// <param name="cancellationToken"></param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(PermissionDelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var value =await permissionway.GetValue(request.Id);
            value.IsDel = true;
            var result = await permissionway.Updateway(value);
            if(result>0)
            {
                res.Message = "删除权限信息成功";
                res.Code = StateCode.OK;
                res.Data = result;
                return res;
            }
            else
            {
                res.Code = StateCode.Fail;
                res.Message = "删除权限信息失败";
                return res;
            }

        }
    }
}
